<?php
App::uses('Sanitize', 'Utility');
class CompetitionsController extends CompetitionsAppController {

	public $uses = array('CompetitionPost','Competition');
	public $helpers = array('Cksource','Js' => array('Jquery')); 
	
	
	public function beforeRender(){
		parent::beforeRender();
		
		Configure::write('competition_domain','http://konkursy.seosolution.pl');
		
		
		if ($this->request->is('ajax')) {
			Configure::write('debug',0);
			$this->disableCache();
			$this->render('ajax');
			$this->layout = 'ajax';
		}else{
			$this->layout = 'competition';
		}
	}
	public function beforeFilter() {
		$this->Auth->allow('install');
		$this->Auth->allow('review');
		$this->Auth->allow('share');
		$this->Auth->allow('end');
		
	}
	
	public function install($id=null) {
		Configure::write('debug',0);
		$this->layout = 'button';
		
		$this->Competition->id = $id;
		if (!$this->Competition->exists()) {
			throw new NotFoundException(__('Invalid competition'));
		}		
		
		$this->request->data['Competition']['id'] = $id;
	}
	
	public function review($id=null) {
		Configure::write('debug',0);
		$this->Competition->id = $id;
		
		if (!$this->Competition->exists()) {
			throw new NotFoundException(__('Invalid competition'));
		}
		$this->layout = 'competition';
		if ($this->request->is('post')) {
			$this->CompetitionPost->create();
			if ($this->CompetitionPost->save($this->request->data)) {
				$this->Session->setFlash(__('The competition post has been saved'));
				$this->redirect(array('action' => 'share',$this->CompetitionPost->id));
			} else {
				$this->Session->setFlash(__('The competition post could not be saved. Please, try again.'));
			}
		}
		
		$this->request->data['CompetitionPost']['product_url'] = $this->referer();
		$this->request->data['CompetitionPost']['competition_id'] = $id;
		$competition = $this->Competition->read(null, $id);
		$this->set('competition',$competition);
	}
	
	public function share($id = null) {

		$this->layout = 'competition';
		$this->CompetitionPost->id = $id;
		if (!$this->CompetitionPost->exists()) {
			throw new NotFoundException(__('Invalid competition post'));
		}
		
		if ($this->request->is('ajax')) {
			if(isset($this->passedArgs['s']) && !empty($this->passedArgs['s'])){
				$this->request->data = $this->CompetitionPost->read(null, $id);
				if($this->passedArgs['s'] == 'f') $this->request->data['CompetitionPost']['soc_facebook'] = 1;
				if($this->passedArgs['s'] == 'g' && $this->passedArgs['state'=='on']) $this->request->data['CompetitionPost']['soc_google'] = 1;
				if($this->passedArgs['s'] == 't') $this->request->data['CompetitionPost']['soc_twitter'] = 1;
				$this->CompetitionPost->id = $this->request->data['CompetitionPost']['id'];
				if($this->CompetitionPost->save($this->request->data)){
					echo 1;
				}else{
					echo 0;
				}
				
			}			
			return;			
		}
		
		if ($this->request->is('post')) {
			if(isset($this->request->data['CompetitionPost']['koniec']) && $this->request->data['CompetitionPost']['koniec'] == '1'){
				$this->redirect(array('action' => 'end',$id));
			}
			
// 			debug($this->request->data); die;
			$this->loadModel('CompetitionUrl');
			$this->CompetitionUrl->set($this->request->data);
			if ($this->CompetitionUrl->validates()) {
				
				$this->CompetitionUrl->create();
				if ($this->CompetitionUrl->save($this->request->data)) {
					$this->Session->setFlash(__('The competition post has been saved'));
					$this->redirect(array('action' => 'share',$id));
				} else {
					$this->Session->setFlash(__('The competition post could not be saved. Please, try again.'));
					$this->redirect(array('action' => 'share',$id));
				}
			}
			
			
		}
		$this->request->data = $this->CompetitionPost->read(null, $id);
		$this->request->data['CompetitionPost']['content'] = trim(Sanitize::html($this->request->data['CompetitionPost']['content'], array('remove' => true)));
		//debug($this->request->data);;
	}
	
	
	public function end($id = null) {
		Configure::write('debug',0);
		$this->layout = 'competition';
		$this->CompetitionPost->id = $id;
		if (!$this->CompetitionPost->exists()) {
			throw new NotFoundException(__('Invalid competition post'));
		}	
		$this->request->data = $this->CompetitionPost->read(null, $id);
	}
}